﻿using EnumsNET;
using Kinlo.Entitys;
using Kinlo.MESDocking;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Measure;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using NewLife.Data;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using UIWindows.Commands;
using UIWindows.GlobalStatics;
using UIWindows.Models;
using UIWindows.Models.Homes;
using UIWindows.Services;
using UIWindows.Tools.Helper;
using XingLucifer.IBase.Enums;
using static log4net.Appender.RollingFileAppender;
using static NPOI.HSSF.Util.HSSFColor;
using static System.Resources.ResXFileRef;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace UIWindows.ViewModels.HomeItemViews
{
    /// <summary>
    /// 组装线前段主页ViewModel
    /// </summary>
    public class FrontProcedureViewModel : UIWindows.Domains.ViewModelBase, XingLucifer.IBase.IViewStatus
    {
        #region 配置显示项
        public GridView ShortCircuitView { get; set; }
        public GridView CoilCoreLoadingDataView { get; set; }
        public GridView BeforeWeightView { get; set; }
        public GridView ProductionDataView { get; set; }
        public GridView ShellInsertionDataView { get; set; }
        public GridView SpotoWeldDataView { get; set; }
        public GridView GrooveRollingMachineDataView { get; set; }
        public GridView ShortCircuitTesterDataView { get; set; }
        public GridView CupSleeveChangingDataView { get; set; }
        public GridView PoleEarFoldingDataView { get; set; }
        public GridView FaceCushionDataView { get; set; }

        #endregion
        public GlobalStatics.ParameterStatic Parameter { get; set; }
        public GlobalStatics.HomeStatic Home { get; set; }
        public GlobalStatics.TemporaryStatic _temporary { get; set; }

        public DeviceInteractiveaddressStatic DeviceInteractiveaddressData { get; set; }
        public SystemConfigStatic _configStatic { get; set; }
        public DisplayItemsStatic _displayItems { get; set; }


        public FrontProcedureViewModel(GlobalStatics.ParameterStatic parameter, GlobalStatics.HomeStatic home, GlobalStatics.TemporaryStatic temporaryStatic, SystemConfigStatic configStatic, DisplayItemsStatic displayItems, DeviceInteractiveaddressStatic deviceInteractiveaddressStatic)
        {
            DeviceInteractiveaddressData = deviceInteractiveaddressStatic;
            Parameter = parameter;
            _temporary = temporaryStatic;
            //parameter.MESCollectionItemsParameter[MachineInfoTypes.卷芯上料].MachineStatus = "0";
            Home = home;
            ShortCircuitView = new GridView();
            CoilCoreLoadingDataView = new GridView();
            ProductionDataView = new GridView();
            ShellInsertionDataView = new GridView();
            SpotoWeldDataView = new GridView();
            GrooveRollingMachineDataView = new GridView();
            ShortCircuitTesterDataView = new GridView();
            CupSleeveChangingDataView=new GridView();
            PoleEarFoldingDataView = new GridView();
            FaceCushionDataView = new GridView();


        }


        public DelegateCommand ClockOutline => new DelegateCommand(() =>
        {

        });

        private List<DisplayItemsModel> _displayItems_ShortCircuit;
        private List<DisplayItemsModel> _displayItems_BeforeWeight;

        public void Load()
        {
            if (_displayItems == null)
            {
                _displayItems = Domains.ObjectContainer.ResolveSingleton<DisplayItemsStatic>();
            }
            if (!_displayItems.IsHomeUpdate)
            {
                return;
            }
            //UpdateProductionDataViewColumns(ProductionDataView);

            ShellInsertionDataView.Columns.Clear();

            CoilCoreLoadingDataView.Columns.Clear();

            SpotoWeldDataView.Columns.Clear();
            GrooveRollingMachineDataView.Columns.Clear();
            ShortCircuitTesterDataView.Columns.Clear();
            CupSleeveChangingDataView.Columns.Clear();
            PoleEarFoldingDataView.Columns.Clear();
            FaceCushionDataView.Columns.Clear();
            UpdateProductionDataViewColumns(ShellInsertionDataView, _displayItems.ShellInsertionDataDisplayItems);

            UpdateProductionDataViewColumns(CoilCoreLoadingDataView, _displayItems.CoilCoreLoadingDataDisplayItems);


            UpdateProductionDataViewColumns(SpotoWeldDataView, _displayItems.SpotoWeldDataDisplayItems);
            UpdateProductionDataViewColumns(GrooveRollingMachineDataView, _displayItems.GrooveRollingMachineDataDisplayItems);
            UpdateProductionDataViewColumns(ShortCircuitTesterDataView, _displayItems.ShortCircuitTesterDataDisplayItems);
            UpdateProductionDataViewColumns(CupSleeveChangingDataView, _displayItems.CupSleeveChangingDataDisplayItems);
            UpdateProductionDataViewColumns(PoleEarFoldingDataView, _displayItems.PoleEarFoldingDataDisplayItems);
            UpdateProductionDataViewColumns(FaceCushionDataView, _displayItems.FaceCushionDataDisplayItems);
            _displayItems.IsHomeUpdate = false;

            //var testData = new CupSleeveChangingData
            //{
            //    AcquisitionTime = 12346,
            //    Operator="ning"
            //};
            //Home.AddBattery(testData, MachineInfoTypes.换套杯);

        }

        private void UpdateProductionDataViewColumns(GridView View, ObservableCollection<DisplayItemsModel> Items)
        {

            if (!_displayItems.IsHomeUpdate)
            {
                return;
            }

            View.Columns.Clear();

            List<DisplayItemsModel> list = new List<DisplayItemsModel>(Items);
            list.Sort((x, y) => x.CompareTo(y));

            foreach (var item in list)
            {
                if (item.IsDisplay)
                {
                    GridViewColumn gvc = new GridViewColumn { Header = item.Description };

                    if (item.DataType == "结果")
                    {
                        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
                        border.SetValue(Border.CornerRadiusProperty, new CornerRadius(3));
                        border.SetBinding(Border.BackgroundProperty, new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.ResultToColorConverter()
                        });

                        FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
                        text.SetValue(TextBlock.MarginProperty, new Thickness(5, 3, 5, 3));
                        text.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.White));
                        text.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                        text.SetBinding(TextBlock.TextProperty, new Binding { Path = new System.Windows.PropertyPath(item.Name) });

                        border.AppendChild(text);
                        var dataTemplate = new DataTemplate();
                        dataTemplate.VisualTree = border;
                        gvc.CellTemplate = dataTemplate;
                    }
                    else if (item.DataType == "浮点型")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "F2"
                        };
                    }
                    else if (item.DataType == "时间型")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "yyyy-MM-dd HH:mm:ss",
                            Converter = new Tools.Converter.TimeStampToDateTimeStringConverter()
                        };
                    }
                    else if (item.DataType == "扫码位置")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.ScanPositionTypeConverter()
                        };
                    }
                    else if (item.DataType == "设备状态")
                    {
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.MachineStatusConverter()
                        };
                    }
                    else
                    {
                        gvc.DisplayMemberBinding = new Binding { Path = new System.Windows.PropertyPath(item.Name) };
                    }

                    View.Columns.Add(gvc);
                }
            }
        }


        public void UnLoad()
        {

        }
        void SetDisplayItemsShortCicute()
        {
            var temp = new MessageShortCircuitModel();
            _displayItems_ShortCircuit = new List<DisplayItemsModel>()
            {
                new DisplayItemsModel(){
                    Index = 1,
                    DataType = "结果",
                    Description = "总结果",
                    Name = nameof(temp.Result) ,//这个双向绑定的属性
                    SqlName = "ALL",//用来区分是什么设备
                },
                new DisplayItemsModel(){
                    Index = 2,
                    Description = "位 置",
                    Name = "Location",
                    SqlName = "ALL",
                },
                new DisplayItemsModel(){
                    Index = 3,
                    Description = "条 码",
                    Name = "BarCode",
                    SqlName = "ALL",
                },
      
            };

        }
        void SetShowShortCicute()
        {

            foreach (var item in _displayItems_ShortCircuit)
            {
                if (item.SqlName == _configStatic.SCM.ShortCircuit.ToString() || item.SqlName == "ALL")
                {
                    GridViewColumn gvc = new GridViewColumn { Header = item.Description };
                    if (item.DataType == "结果")
                    {
                        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
                        border.SetValue(Border.CornerRadiusProperty, new CornerRadius(3));
                        border.SetBinding(Border.BackgroundProperty, new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            Converter = new Tools.Converter.ResultToColorConverter()
                        });

                        FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
                        text.SetValue(TextBlock.MarginProperty, new Thickness(5, 3, 5, 3));
                        text.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.White));
                        text.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                        text.SetBinding(TextBlock.TextProperty, new Binding { Path = new System.Windows.PropertyPath(item.Name) });

                        border.AppendChild(text);
                        var dataTemplate = new DataTemplate();
                        dataTemplate.VisualTree = border;
                        gvc.CellTemplate = dataTemplate;
                    }
                    else if (item.DataType == "浮点型")
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "F2"
                        };
                    else if (item.DataType == "时间型")
                        gvc.DisplayMemberBinding = new Binding
                        {
                            Path = new System.Windows.PropertyPath(item.Name),
                            StringFormat = "yyyy-MM-dd HH:mm:ss",
                            Converter = new Tools.Converter.TimeStampToDateTimeStringConverter()
                        };
                    else
                        gvc.DisplayMemberBinding = new Binding { Path = new System.Windows.PropertyPath(item.Name) };

                    ShortCircuitView.Columns.Add(gvc);
                }
            }
        }
        void SetDisplayItemsBeforeWeight()
        {
            var temp = new MessageBeforeWeighingModel();
            if (_configStatic.SCM.MainDeviceType == MainDeviceTypeEnum.后段组装线)
            {
                _displayItems_BeforeWeight = new List<DisplayItemsModel>
                 {
                         new DisplayItemsModel()
                         {
                             Index = 1,
                             DataType = "结果",
                             Description = "结 果",
                             Name =nameof(temp.Result),//这个双向绑定的属性 
                         },
                         new DisplayItemsModel()
                         {
                             Index = 2,
                             Description = "条 码",
                             Name =nameof(temp.BarCode),//这个双向绑定的属性 
                         },
                         new DisplayItemsModel()
                         {
                             Index = 3,
                             //DataType = "时间型",
                             Description = "前称时间",
                             Name =nameof(temp.Time),//这个双向绑定的属性 
                         },
                         new DisplayItemsModel()
                         {
                             Index = 4,
                             DataType = "浮点型",
                             Description = "前称重量",
                             Name =nameof(temp.FormerWeighing),//这个双向绑定的属性 
                         },
                          new DisplayItemsModel()
                         {
                             Index = 5,
                             Description = "前称重位置",
                             Name =nameof(temp.LocationFormerWeighing),//这个双向绑定的属性 
                         },
                 };
            }
            else
            {
                _displayItems_BeforeWeight = new List<DisplayItemsModel>
                 {
                         new DisplayItemsModel()
                         {
                             Index = 1,
                             DataType = "结果",
                             Description = "结 果",
                             Name =nameof(temp.Result),//这个双向绑定的属性 
                         },
                         new DisplayItemsModel()
                         {
                             Index = 2,
                             Description = "条 码",
                             Name =nameof(temp.BarCode),//这个双向绑定的属性 
                         },
                         new DisplayItemsModel()
                         {
                             Index = 3,
                            // DataType = "时间型",
                             Description = "前称时间",
                             Name =nameof(temp.Time),//这个双向绑定的属性 
                         },
                         new DisplayItemsModel()
                         {
                             Index = 4,
                              DataType = "浮点型",
                             Description = "前称重量",
                             Name =nameof(temp.FormerWeighing),//这个双向绑定的属性 
                         },
                          new DisplayItemsModel()
                         {
                             Index = 5,
                             Description = "前称重位置",
                             Name =nameof(temp.LocationFormerWeighing),//这个双向绑定的属性 
                         },
                           new DisplayItemsModel()
                         {
                             Index = 8,
                              DataType = "浮点型",
                             Description = "目标注液量",
                             Name =nameof(temp.TargetInjectionVolume),//这个双向绑定的属性 
                         },
                            new DisplayItemsModel()
                         {
                             Index = 8,
                              DataType = "浮点型",
                             Description = "化成失液量",
                             Name =nameof(temp.LossAfterFormation),//这个双向绑定的属性 
                         },
                            new DisplayItemsModel()
                         {
                             Index = 7,
                              DataType = "浮点型",
                             Description = "电池干重",
                             Name =nameof(temp.NetWeight),//这个双向绑定的属性 
                         },
                             new DisplayItemsModel()
                         {
                             Index = 8,
                              DataType = "浮点型",
                             Description = "一注后称重",
                             Name =nameof(temp.OneAfterWeighing),//这个双向绑定的属性 
                         },
                 };
            }
        }

        public DelegateCommand ClickMe => new DelegateCommand(() =>
        {
            MySqlHelper mySqlHelper = new MySqlHelper(Parameter.MESParameter.OthenDatabaseurl);
            var oneData = mySqlHelper.GetBatteryByCode("Empty83564123456789024720");
            if (oneData.status)
            {
                var NetWeight = oneData.netWeight;
                var OneAfterWeighing = oneData.outputWeight;
            }
        });
        void SetShowBeforWeight()
        {
            foreach (var item in _displayItems_BeforeWeight)
            {
                GridViewColumn gvc = new GridViewColumn { Header = item.Description };
                if (item.DataType == "结果")
                {
                    FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
                    border.SetValue(Border.CornerRadiusProperty, new CornerRadius(3));
                    border.SetBinding(Border.BackgroundProperty, new Binding
                    {
                        Path = new System.Windows.PropertyPath(item.Name),
                        Converter = new Tools.Converter.ResultToColorConverter()
                    });

                    FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
                    text.SetValue(TextBlock.MarginProperty, new Thickness(5, 3, 5, 3));
                    text.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.White));
                    text.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                    text.SetBinding(TextBlock.TextProperty, new Binding { Path = new System.Windows.PropertyPath(item.Name) });

                    border.AppendChild(text);
                    var dataTemplate = new DataTemplate();
                    dataTemplate.VisualTree = border;
                    gvc.CellTemplate = dataTemplate;
                }
                else if (item.DataType == "浮点型")
                    gvc.DisplayMemberBinding = new Binding
                    {
                        Path = new System.Windows.PropertyPath(item.Name),
                        StringFormat = "F2"
                    };
                else if (item.DataType == "时间型")
                    gvc.DisplayMemberBinding = new Binding
                    {
                        Path = new System.Windows.PropertyPath(item.Name),
                        StringFormat = "yyyy-MM-dd HH:mm:ss",
                        Converter = new Tools.Converter.TimeStampToDateTimeStringConverter()
                    };
                else
                    gvc.DisplayMemberBinding = new Binding { Path = new System.Windows.PropertyPath(item.Name) };

                BeforeWeightView.Columns.Add(gvc);
            }


        }
        public DelegateCommand TestCMD => new DelegateCommand(() =>
        {
            int b = 239;
            double c = (double)b / 240;
            double a = Math.Round(c, 4, MidpointRounding.ToNegativeInfinity);
            a = a * 100;
            $"{a}".MessageDialogs(MessageLevelType.信息, false, Enums.PopUpTypes.Global);
        });
    }

}
